//
//  AnalysisJson.m
//  Finance
//
//  Created by cheng xiang on 13-4-8.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "AnalysisJson.h"

@implementation AnalysisJson

//标准json:{key:value,key:value,subObject:[{key:value,key:value},{key:value}]}

//objectClass为外层的对象，node为嵌套的数组内的对象同时也为外层对象的嵌套数组名称
+ (id ) analysisJsonToObject:(Class) objectClass fromJsonStr: (NSString *)jsonStr
{
    NSError *error;
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSObject *obj = [[objectClass alloc] init];
    if ([json isKindOfClass:[NSDictionary class]]) {
        //有公共字段和明细数组的json
        NSDictionary *dic = (NSDictionary *)json;
        for (int i = 0; i < [dic.allKeys count]; i++) {
            NSString *key = (dic.allKeys)[i];
            id str = dic[key];
            [obj setValue:str forKey:key];
        }
        return obj;
    }else if ([json isKindOfClass:[NSArray class]]) {
        //只有明细数组的json,暂时不做处理，先看接口的定义
//        NSMutableArray *results = [[NSMutableArray alloc] init];
//        NSArray *array = (NSArray *)json;
//        for (int i = 0; i < [array count]; i++) {
//            NSDictionary *dic = [array objectAtIndex:i];
//            NSObject *obj = [[objectClass alloc] init];
//            for (int j = 0; j < [dic.allKeys count]; j++) {
//                NSString *str = [dic objectForKey:[dic.allKeys objectAtIndex:j]];
//                [obj setValue:str forKey:[dic.allKeys objectAtIndex:j]];
//            }
//            [results addObject:obj];
//        }
//        return results;
    }
    return nil;
}

+ (NSString *) analysisJsonToString: (NSString *)keyStr fromJsonStr: (NSString *)jsonStr
{
    
    return nil;
}

+ (NSString *) PackageJsonFromObject: (id)data
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&error];
    if ([jsonData length] > 0 && error == nil) {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return jsonString;
    }else{
        return nil;
    }
}

+ (NSDictionary *) PackageJsonHeader: (NSString *)transtype
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"version"] = HEADER_VERSION;
    dic[@"transType"] = transtype;
    
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    date = [formatter stringFromDate:[NSDate date]];
    
    dic[@"submitTime"] = date;
    dic[@"clientTraceNo"] = date;
    dic[@"appId"] = APPID;
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    dic[@"appVersion"] = delegate.app_version;
    dic[@"deviceId"] = date;
    dic[@"deviceType"] = @"03";
    dic[@"deviceOS"] = @"iOS";
    dic[@"deviceOSVersion"] = [NSString stringWithFormat:@"%f",delegate.version];
    dic[@"deviceModel"] = @"Apple iPhone";
    dic[@"signMessage"] = date;
    
    NSMutableDictionary *headerDic = [[NSMutableDictionary alloc] init];
    headerDic[@"header"] = dic;
    
    return headerDic;
}

+ (NSData *) PackageJsonFromHeader: (NSDictionary *)header andBody:(NSDictionary *)body
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *bodyDic = [[NSMutableDictionary alloc] init];
    bodyDic[@"body"] = body;
    [dic addEntriesFromDictionary:header];
    [dic addEntriesFromDictionary:bodyDic];
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    if ([jsonData length] > 0 && error == nil) {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSString *tmp = [@"inputJson=" stringByAppendingString:jsonString];
        NSLog(@"%@",tmp);
        jsonData = [tmp dataUsingEncoding:NSUTF8StringEncoding];
        return jsonData;
    }else{
        return nil;
    }
}

@end
